//
//  KsViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/9/2.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class KsViewController: NSViewController {
    
    var sTitle:String = ""
    var msg:String = ""
    var btnTitles:[String] = []
    var touchAction:ResultInt?
    
    var subBtns:[ZHButton] = []
    var kWidth:CGFloat = 300
    var kBtnWidth:CGFloat = 80
    var kBtnHeight:CGFloat = 26
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 420, 180))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    convenience init(title:String,msg:String,btnTitles:[String],touchAction:ResultInt?) {
        self.init()
        self.sTitle = title
        self.msg = msg
        self.btnTitles = btnTitles
        self.touchAction = touchAction
        self.setupUI()
        self.loadData()
    }
    func loadData() {
        self.sTitleLab.stringValue = self.sTitle
        self.contentLab.stringValue = self.msg
    }
    
    func setupUI() {
        
        self.view.addSubview(sTitleLab)
        sTitleLab.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(30)
        }
        
        self.view.addSubview(contentLab)
        contentLab.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.centerX.equalToSuperview()
            make.top.equalTo(sTitleLab.snp.bottom).offset(20)
            make.width.equalTo(300)
        }
        
        self.view.addSubview(containr)
        containr.snp.makeConstraints { (make) in
            make.width.equalTo(kWidth)
            make.bottom.equalTo(-20)
            make.height.equalTo(30)
            make.centerX.equalToSuperview()
        }
        
        self.subBtns = self.initView(inView: containr, titles:self.btnTitles)
    }
    
    lazy var sTitleLab: NSLabel = {
        let lab = NSLabel.label(title: "标题", textColor: NSColor.black, font: NSFont.systemFont(ofSize: 16))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
    
    
    lazy var contentLab: NSLabel = {
        let lab = NSLabel.label(title: "内容", textColor: color_gray(180), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .center
        lab.maximumNumberOfLines = 2
        return lab
    }()
    
    lazy var containr: NSView = {
        let view = NSView()
        return view
    }()
    
    func initView(inView:NSView,titles:[String]) -> [ZHButton] {
        let space = (self.kWidth-CGFloat(titles.count*80))/CGFloat(titles.count-1)
        var subBtns:[ZHButton] = []
        for (inx,val) in titles.enumerated() {
            let btn = ZHButton.init()
            btn.title = val
            btn.font = NSFont.systemFont(ofSize: 12)
            if inx == 0 {
                btn.zh_color = color_main140
                btn.zh_backgroundColor = NSColor.white
                btn.backgroundHoverColor = color_gray(240)
                btn.zh_borderWidth = 1.0
                btn.zh_borderColor = color_gray(220)
                btn.hasBorder = true
            }else {
                btn.zh_color = NSColor.white
                btn.zh_backgroundColor = color_main140
                btn.backgroundHoverColor = color_main140.withAlphaComponent(0.8)
            }
            btn.tag = inx
            btn.target = self
            btn.action = #selector(selectBtnAction(senber:))
            inView.addSubview(btn)
            subBtns.append(btn)
            
            btn.snp.makeConstraints { (make) in
                make.height.equalTo(26)
                make.width.equalTo(80)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(subBtns[inx-1].snp.right).offset(space)
                }
            }
        }
        return subBtns
    }
    
    
    @objc func selectBtnAction(senber:ZHButton) {
        self.dismiss(nil)
        if touchAction != nil {
            self.touchAction!(senber.tag)
        }
    }
}
